package com.bite.bite_forum.controller;
import com.bite.bite_forum.common.AppConfig;
import com.bite.bite_forum.common.AppResult;
import com.bite.bite_forum.common.ResultCode;
import com.bite.bite_forum.model.Article;
import com.bite.bite_forum.model.ArticleReply;
import com.bite.bite_forum.model.User;
import com.bite.bite_forum.services.IArticleService;
import com.bite.bite_forum.services.Impl.ArticleReplyServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Api(tags = "回复接口")
@RequestMapping("/reply")
@Slf4j
public class ArticleReplyController {
   @Resource
    ArticleReplyServiceImpl articleReplyService;
   @Resource
    IArticleService articleService;

   @ApiOperation("新建回复")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                           @ApiParam("帖子id") @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam("回复正文") @RequestParam("content") @NonNull String content){
        //判断用户是否被禁言
       HttpSession session = request.getSession();
       User user = (User) session.getAttribute(AppConfig.USER_SESSION);
       if(user.getState() == 1){
           log.info(ResultCode.FAILED_USER_BANNED.getMessage());
           return AppResult.failed(ResultCode.FAILED_USER_BANNED);
       }

       //判断帖子是否存在
       Article article = articleService.selectById(articleId);
       if(article == null || article.getDeleteState() == 1){
           log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
           return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
       }

       //帖子状态
       if(article.getState() == 1){
           log.info(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
           return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
       }
       //新建回复
       ArticleReply articleReply = new ArticleReply();
       articleReply.setArticleId(articleId);
       articleReply.setContent(content);
       articleReply.setPostUserId(user.getId());
       articleReplyService.create(articleReply);
       return AppResult.success();

    }

    @ApiOperation("获取回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam("帖子id")
                                                                   @RequestParam("articleId") @NonNull Long articleId){
       Article article = articleService.selectById(articleId);
        if(article == null || article.getDeleteState() == 1){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.getMessage());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        //帖子状态
        if(article.getState() == 1){
            log.info(ResultCode.FAILED_ARTICLE_BANNED.getMessage());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        List<ArticleReply> result = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(result);
    }
}
